/*
 * ModifyLineAction.java
 * 
 * Created by demory on Apr 1, 2009, 7:45:04 AM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.actions;

import org.fpdev.apps.cart.Cartographer;
import org.fpdev.apps.cart.Editor;
import org.fpdev.apps.cart.network.Corridor;
import org.fpdev.apps.cart.network.Line;

/**
 *
 * @author demory
 */
public class ModifyLineAction extends EditorBasedAction {

  private Line line_;
  private boolean added_;
  private Corridor corr_;

  public ModifyLineAction(Editor ed, Line line, Corridor corr) {
    super(ed);
    line_ = line;
    corr_ = corr;
  }

  public boolean doAction(Cartographer cart) {
    boolean success = false;
    if(corr_ == line_.firstCorridor() || corr_ == line_.lastCorridor()) {
      line_.removeCorridor(corr_);
      success = true;
      added_ = false;
    }
    else {
      success = line_.addCorridor(corr_);
      added_ = true;
    }

    if(success) ed_.getDocument().getNetwork().rebundle();
    return success;
  }

  public boolean undoAction(Cartographer cart) {
    boolean success = false;
    if(added_) success = line_.removeCorridor(corr_);
    else success = line_.addCorridor(corr_);

    if(success) ed_.getDocument().getNetwork().rebundle();
    return success;
  }

  public String getName() {
    return "Modify Line";
  }


}
